{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "from lxml import etree"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "url = 'http://daili.spbeen.com/user_info/person/'\n",
    "headers = {\n",
    "    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36',\n",
    "    'Cookie': 'UM_distinctid=173b22160a21d4-016d5e2a30dfa2-5f1a391c-1fa400-173b22160a3644; CNZZDATA1274503826=51721534-1596528468-%7C1596678278; csrftoken=PYanHwFCwewlvmGPK16o5GVHC2LCbjxXzR9EcdZcOZ63OhhDy7AzB6PSXeLFq2Sv; _dfesp=enwxoswa1x8q0radf08w6vtj8u3hf50c'\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "response = requests.get(url, headers=headers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'<!DOCTYPE html>\\n<html>\\n\\n<head>\\n    <meta charset=\"utf-8\">\\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\\n    <title>详细资料|个人信息</title>\\n    <link rel=\"stylesheet\" href=\"https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.1/css/bulma.min.css\">\\n\\n<link rel=\"stylesheet\" href=\"https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css\">\\n\\n<script defer src=\"https://use.fontawesome.com/releases/v5.1.0/js/all.js\"></script>\\n\\n\\n\\n<script\\n  src=\"https://code.jquery.com/jquery-1.12.4.min.js\"\\n  integrity=\"sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=\"\\n  crossorigin=\"anonymous\"></script>\\n<script src=\"https://cdn.jsdelivr.net/npm/vue/dist/vue.js\"></script>\\n\\n<script src=\"/static/js/jquery.qrcode.min.js\"></script>\\n<script src=\"/static/js/vue-qrcode.min.js\"></script>\\n\\n    <style>\\n        p.ips{\\n            text-align: center;\\n            background-color: #23d160;\\n            border-radius: 4px;\\n            color: white;\\n        }\\n\\n    </style>\\n</head>\\n\\n<body>\\n    <nav class=\"navbar is-black is-fixed-top\" role=\"navigation\" aria-label=\"main navigation\">\\n    <div class=\"container\">\\n        <div class=\"navbar-brand\">\\n            \\n            <a class=\"navbar-item\" href=\"/\">\\n            \\n                聚合代理\\n            </a>\\n            <a role=\"button\" class=\"navbar-burger\" aria-label=\"menu\" aria-expanded=\"false\" style=\"color:#f5f5f5;\">\\n              <span aria-hidden=\"true\"></span>\\n              <span aria-hidden=\"true\"></span>\\n              <span aria-hidden=\"true\"></span>\\n            </a>\\n\\n        </div>\\n\\n        <div class=\"navbar-menu\">\\n            <div class=\"navbar-end\">\\n                \\n                    <a class=\"navbar-item is-right\" href=\"/user_info/person\">\\n                      royee\\n                    </a>\\n                    <a class=\"navbar-item is-right\" href=\"/logout\">\\n                      退出\\n                    </a>\\n                \\n            </div>\\n        </div>\\n    </div>\\n</nav>\\n\\n\\n    <div class=\"container\" style=\"margin-top: 60px;\">\\n\\n\\n        <div class=\"columns is-centered\">\\n\\n            <div class=\"column\">\\n    <div class=\"tabs is-centered\">\\n      <ul>\\n        <li  class=\"is-active\" ><a href=\"/user_info/person\">详细资料</a></li>\\n        <li ><a href=\"/user_info/balance\">余量变化</a></li>\\n\\n        <li ><a href=\"/user_info/pay\">充值记录</a></li>\\n        <li >\\n            <a href=\"/user_info/mail\">\\n                邮件服务\\n                <span class=\"tag is-warning is-small\">Beta</span>\\n            </a>\\n          </li>\\n      </ul>\\n    </div>\\n</div>\\n\\n        </div>\\n\\n\\n\\n\\n\\n\\n        <div class=\"columns is-mobile is-centered\">\\n\\n          <div class=\"column is-10\">\\n\\n\\n\\n              \\n                <div class=\"notification is-warning is-small animated fadeInDown\">\\n                【默认】欢迎来到本站，我们提供极具性价比的高质量代理IP\\n                </div>\\n\\n            \\n              <table class=\"table is-fullwidth is-striped animated fadeIn\">\\n                  <thead>\\n                  <tr><th>属性</th><th>值</th></tr>\\n                  </thead>\\n                  <tbody>\\n                    <tr><td>账号</td><td>royee</td></tr>\\n\\n                    <tr><td>令牌(Token)</td><td>Ufwmkif9GAXh49Rb1pLqY9MB  <a class=\"button is-danger is-small\" href=\"/update_token\">更新</a></td></tr>\\n\\n\\n                    <tr><td>余量</td><td>199个 <button id=\"show_charge\" class=\"button is-warning is-small\">充值</button></td></tr>\\n\\n\\n\\n                    <tr><td>今日剩余请求</td><td>998个</td></tr>\\n                    \\n                    <tr>\\n                        <td>白名单IP</td>\\n                        <td>\\n                            114.95.120.147\\n                            \\n                        </td>\\n                    </tr>\\n                    \\n                    <tr><td>请求间隔(秒)</td><td>2秒</td></tr>\\n                    <tr><td>上次登录时间</td><td>2020-08-06 09:45:24</td></tr>\\n\\n                    <tr><td>单次请求IP个数</td><td>GET请求参数num，默认一个，范围1-10【固定】</td></tr>\\n                    <tr>\\n                        <td>请求链接 </td>\\n                        <td>\\n                            <code class=\"html\">http://daili.spbeen.com/get_api_json?token=Ufwmkif9GAXh49Rb1pLqY9MB&num=1</code>\\n                            <a class=\"button is-info is-small\" href=\"http://daili.spbeen.com/get_api_json?token=Ufwmkif9GAXh49Rb1pLqY9MB&num=1\" target=\"_blank\">点击测试</a>\\n                        </td>\\n                    </tr>\\n\\n\\n\\n\\n                  <tr>\\n                      <td>更多操作</td>\\n                      <td>\\n                          <a id=\"show_reward\" class=\"button is-success is-small\">领取余量</a>\\n                          \\n                      </td>\\n\\n                  </tr>\\n                  </tbody>\\n\\n              </table>\\n          </div>\\n        </div>\\n        </div>\\n\\n    <div id=\"charge\" class=\"modal\">\\n      <div class=\"modal-background animated fadeInUp\"></div>\\n\\n      <div class=\"modal-card animated fadeInDown\">\\n        <header class=\"modal-card-head\">\\n          <p class=\"modal-card-title\">请选择充值量，赠送 20个/元 余量</p>\\n\\n          <button id=\"hide_charge\" class=\"delete\" aria-label=\"close\" type=\"button\"></button>\\n        </header>\\n        <section class=\"modal-card-body\">\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n                 <form action=\"/create_order/\" method=\"post\">\\n                    <div class=\"field\">\\n                        <div class=\"control\">\\n                            充值余量：\\n                            <label class=\"radio\">\\n                                <input type=\"radio\" name=\"price\" checked value=\"2\">\\n                                160个/2元\\n                              </label>\\n                              <label class=\"radio\">\\n                                <input type=\"radio\" name=\"price\" value=\"5\">\\n                                400个/5元\\n                              </label>\\n                              <label class=\"radio\">\\n                                <input type=\"radio\" name=\"price\" value=\"10\">\\n                                1000个/10元\\n                              </label>\\n                        </div>\\n                    </div>\\n                    <div class=\"field\">\\n                        <div class=\"control\">\\n                            支付方式：\\n\\n\\n\\n\\n                          <label class=\"radio\">\\n                            <input type=\"radio\" name=\"pay_type\" checked value=\"2\">\\n                            微信\\n                          </label>\\n                        </div>\\n                    </div>\\n                    <div class=\"field\">\\n\\n                    </div>\\n                    <input type=\\'hidden\\' name=\\'csrfmiddlewaretoken\\' value=\\'dbpjlz5YM1ErtmP6yBNi45N5Bc3AGfVKX4oAQgpy4Me9MhqUmHhtAvHgWo3DVYgi\\' />\\n                    <div class=\"field\">\\n                        <p class=\"control\">\\n                            <button type=\"submit\" class=\"button is-info\">\\n                                充值\\n                            </button>\\n                        </p>\\n                    </div>\\n                </form>\\n\\n\\n\\n\\n\\n        </section>\\n        <footer class=\"modal-card-foot\">\\n            提供学员少量的代理IP，请联系助教海哥，QQ：178-240-0450\\n\\n        </footer>\\n\\n\\n      </div>\\n        <script type=\"text/javascript\">\\n            $(\"#show_charge\").click(function(){\\n              $(\"#charge\").addClass(\"is-active\");\\n            });\\n            $(\"#hide_charge\").click(function(){\\n              $(\"#charge\").removeClass(\"is-active\");\\n            });\\n        </script>\\n\\n\\n\\n\\n\\n\\n    </div>\\n\\n\\n    <div class=\"modal\" id=\"reward_allowance\" >\\n      <div class=\"modal-background\"></div>\\n      <div class=\"modal-card\">\\n        <header class=\"modal-card-head\">\\n          <p class=\"modal-card-title\">请输入余量码</p>\\n          <button id=\"del_reward\" class=\"delete\" aria-label=\"close\"></button>\\n        </header>\\n        <section class=\"modal-card-body\">\\n            <form action=\"/check_giftcoupon/\" method=\"post\">\\n                <div class=\"field\">\\n                <div class=\"control\">\\n                <input name=\"giftcoupon\" class=\"input\" type=\"text\" placeholder=\"请输入余量码\" autocomplete=\"off\">\\n                </div></div>\\n                <input type=\\'hidden\\' name=\\'csrfmiddlewaretoken\\' value=\\'dbpjlz5YM1ErtmP6yBNi45N5Bc3AGfVKX4oAQgpy4Me9MhqUmHhtAvHgWo3DVYgi\\' />\\n                <div class=\"field\">\\n                <div class=\"control\">\\n                    <button class=\"button is-success\" type=\"submit\">提交</button>\\n                </div></div>\\n            </form>\\n        </section>\\n        <footer class=\"modal-card-foot\">\\n            一个余量码只能被一个账户使用一次 ， 账户可以使用无数次不同余量码\\n\\n\\n        </footer>\\n      </div>\\n    </div>\\n        <script type=\"text/javascript\">\\n            $(\"#show_reward\").click(function(){\\n              $(\"#reward_allowance\").addClass(\"is-active\");\\n            });\\n            $(\"#hide_reward\").click(function(){\\n              $(\"#reward_allowance\").removeClass(\"is-active\");\\n            });\\n            $(\"#del_reward\").click(function(){\\n              $(\"#reward_allowance\").removeClass(\"is-active\");\\n            });\\n        </script>\\n\\n\\n\\n    \\n\\n<script type=\"text/javascript\">\\n    $(\"img.captcha\").click(function(){   //更新验证码图片ajax\\n        $.getJSON(\"/captcha/refresh\",function(data){\\n            $(\"img.captcha\").attr(\"src\",data.image_url);\\n            $(\"#id_captcha_0\").attr(\"value\",data.key);\\n        });\\n    });\\n    $(document).ready(function() {\\n      // Check for click events on the navbar burger icon\\n      $(\".navbar-burger\").click(function() {\\n\\n          // Toggle the \"is-active\" class on both the \"navbar-burger\" and the \"navbar-menu\"\\n          $(\".navbar-burger\").toggleClass(\"is-active\");\\n          $(\".navbar-menu\").toggleClass(\"is-active\");\\n\\n      });\\n    });\\n</script>\\n<footer class=\"footer is-dark\" style=\"padding: 20px;bottom:0px;margin-top:50px;\">\\n  <div class=\"content has-text-centered\">\\n      <p> <i>聚合代理</i> 仅提供大数据采集服务，用户使用 <i>聚合代理</i> 资源从事的任何行为均不代表本站的意志和观点，与 <i>聚合代理</i> 的立场无关。</p>\\n      <p>严禁用户使用 <i>聚合代理</i> 资源从事任何违法犯罪行为，产生的相关责任用户自负，对此 <i>聚合代理</i> 不承担任何法律责任。 </p>\\n      <p>Copyright @2016-2020 | 赣ICP备16003025号 <script src=\"https://s22.cnzz.com/z_stat.php?id=1274503826&web_id=1274503826\" language=\"JavaScript\"></script></p>\\n\\n  </div>\\n</footer>\\n</body>\\n\\n</html>'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response.text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "html = etree.HTML(response.text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['royee'] ['Ufwmkif9GAXh49Rb1pLqY9MB  '] ['998个']\n"
     ]
    }
   ],
   "source": [
    "user = html.xpath('.//td[contains(text(), \"账号\")]/../td[2]/text()')\n",
    "token = html.xpath('.//td[contains(text(), \"令牌\")]/../td[2]/text()')\n",
    "shengyu = html.xpath('.//td[contains(text(), \"剩余\")]/../td[2]/text()')\n",
    "print(user, token, shengyu)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 总结：\n",
    "- 登录状态，是使用的Cookie字段的记录\n",
    "- Cookie是请求头中的字段，并且请求头中的字段，首字母大写\n",
    "- 不携带Cookie，未登录状态下访问的页面，则默认会跳转到登录页面\n",
    "- requests会自动的解决跳转问题，并且跳转的记录，保存在history属性内，状态为302"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
